<?php
/**
 * 此文件是接口程序路由
 *
 *
 * 
 * 
 */


/**
 * 程序路由处理类
 *
 * 这里类判断外界参数调用内部方法
 */
class Application
{
    var $controller 		= null;  		//类名
    var $action				= null;  		//方法名
    var $default_controller = null;   		//默认的类名
    var $default_do         = null;     	//默认的方法名
    var $controller_var     = 'c';  		//Post or Get 控制器变量
    var $action_var         = 'd';			//Post or Get 函数变量
    var $sub_dir            = '';			//控制器子目录

    /**
     * 设置默认的类名
     *
     * @param string $default_controller
     */
    function setDefaultController($default_controller)
    {
        $this->default_controller = $default_controller;
    }

    /**
     * 设置默认的方法名
     *
     * @param string $default_action
     */
    function setDefaultAction($default_action)
    {
        $this->default_action = $default_action;
    }

    /**
     * 设置控制器子目录
     *
     * @param string $dir
     */
    function setSubDir($dir)
    {
        $this->sub_dir = $dir;
    }

    //运行自动加载的控制器
    function autorun()
    {
        global $sys_conf_var;

        //运行自动加载的控制器开始
        if(count($sys_conf_var['autorun']) > 0)
        {
            foreach ($sys_conf_var['autorun'] as $key => $var)
            {
                $sub_dir = $this->sub_dir ? $this->sub_dir . '/' : '';

                $class_file = CONTROLLER_DIR . $sub_dir . $var['controller'] . '.class.php';

                if(!is_file($class_file))
                {
                    throw new AppException('Invalid controller ' . $var['controller']);
                }

                require_once $class_file;

                $class = ucfirst($var['controller']);

                if(!class_exists($class))
                {
                    throw new AppException('Invalid Class ' . $class);
                }

                $instance = new $class();

                $function = $var['function'];

                if(!method_exists($instance,$function))
                {
                    throw new AppException('Invalid Function ' . $function);
                }

                $instance->tpl->assign('controller', $var['controller']);
                $instance->tpl->assign('action', $var['function']);
                $instance->$function();
            }
        }

        //运行自动加载的控制器结束
    }

    /**
     * 调用实际类和方式
     *
     */
    function run()
    {
        global $in_var;

        $this->autorun();

        $this->controller = $in_var[$this->controller_var] ? $in_var[$this->controller_var] : $this->default_controller;
        $this->action     = $in_var[$this->action_var] ? $in_var[$this->action_var] : $this->default_action;

        if(!$this->controller || !$this->action) throw new AppException('The lack of parameters');

        $sub_dir = $this->sub_dir ? $this->sub_dir . '/' : '';

        $class_file = CONTROLLER_DIR . $sub_dir . $this->controller . '.class.php';

        if(!is_file($class_file))
        {
            throw new AppException('Invalid controller ' . $this->controller);
        }

        require_once $class_file;

        $class = ucfirst($this->controller);

        if(!class_exists($class))
        {
            throw new AppException('Invalid Class ' . $class);
        }

        $instance = new $class();

        $function = $this->action;

        if(!method_exists($instance,$function))
        {
            throw new AppException('Invalid Function ' . $function);
        }

        $instance->tpl->assign('controller', $this->controller);
        $instance->tpl->assign('action', $function);
        $instance->$function();
    }
}


/**
 * $Log$
 */
?>
